﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace VicMessenger.UserControls
{
    /// <summary>
    /// Interaction logic for VicNavCtl.xaml
    /// </summary>
    public partial class VicNavCtl : UserControl
    {
        public event EventHandler<EventArgs> FirstButton;
        public event EventHandler<EventArgs> PrevButton;
        public event EventHandler<EventArgs> NextButton;
        public event EventHandler<EventArgs> LastButton;

      

        public static DependencyProperty CurrentPositionProperty =
                               DependencyProperty.Register("CurrentPosition", typeof(int), typeof(VicNavCtl), new PropertyMetadata(1));
        public int CurrentPosition
        {
            get { return (int)GetValue(CurrentPositionProperty); }
            set { SetValue(CurrentPositionProperty, value); }
        }



        public static DependencyProperty TotalPagesProperty =
                       DependencyProperty.Register("TotalPages", typeof(int), typeof(VicNavCtl), new PropertyMetadata(1));
        public int TotalPages
        {
            get { return (int)GetValue(TotalPagesProperty); }
            set { SetValue(TotalPagesProperty, value); }
        }

        public VicNavCtl()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(VicNavCtl_Loaded);
        }

        void VicNavCtl_Loaded(object sender, RoutedEventArgs e)
        {

            //Binding bnd = new Binding("CurrentPosition");
            //bnd.Mode = BindingMode.TwoWay;
            //txtStatus.SetBinding(TextBox.TextProperty, bnd);
        }

        private void btnFirst_Click(object sender, RoutedEventArgs e)
        {
            e.Handled = true;
            if (FirstButton != null)
            {
                UpdatePosition("first");
                FirstButton(this, e);
            }
        }

        private void btnPrev_Click(object sender, RoutedEventArgs e)
        {
            e.Handled = true;
            if (PrevButton != null)
            {
                UpdatePosition("prev");
                PrevButton(this, e);
            }
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            e.Handled = true;
            if (NextButton != null)
            {
                UpdatePosition("next");
                NextButton(this, e);
            }
        }

        private void btnLast_Click(object sender, RoutedEventArgs e)
        {
            e.Handled = true;
            if (LastButton != null)
            {
                UpdatePosition("last");
                CurrentPosition = TotalPages;
                LastButton(this, e);
            }
        }

        private void UpdatePosition(string prmStep)
        {
            try
            {
                if (TotalPages == 0) return;
                switch (prmStep.Trim().ToLower())
                {
                    case "first":
                        CurrentPosition = 0;
                        break;
                    case "prev":
                        CurrentPosition = (CurrentPosition - 1) % TotalPages;
                        break;
                    case "next":
                        CurrentPosition = (CurrentPosition + 1) % TotalPages;
                        break;
                    case "last":
                        CurrentPosition = 1;
                        break;
                    default:
                        break;
                }

                txtStatus.Text = CurrentPosition.ToString();
            }
            catch (Exception)
            {

            }
        }
    }
}
